package chendu.controller;

import chendu.entity.PhoneInfo;
import chendu.entity.PhoneSpecs;
import chendu.service.PhoneService;
import chendu.util.ResultVOUtil;

import chendu.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/phone")
public class PhoneHandler {

    @Autowired
    private PhoneService phoneService;

    //页面首页
    @GetMapping("/index")
    public ResultVO index(){
        return ResultVOUtil.success(phoneService.findDataVO());
    }

    //根据手机类型查询相应的手机
   @GetMapping("/findByCategoryType/{categoryType}")
    public ResultVO findByCategoryType(
            @PathVariable("categoryType") Integer categoryType){
        return ResultVOUtil.success(phoneService.findPhoneInfoVOByCategoryType(categoryType));
    }

    //根据手机id查询对应的手机
    @GetMapping("/findSpecsByPhoneId/{phoneId}")
    public ResultVO findSpecsByPhoneId(
            @PathVariable("phoneId") Integer phoneId){
        return ResultVOUtil.success(phoneService.findSpecsByPhoneId(phoneId));
    }
    //根据手机Id查询手机规格
    @GetMapping("/findSpecsId")
    public PhoneSpecs findSpecsById(Integer specsId){
        return phoneService.findSpecsById(specsId);
    }
    // 调用phone查询phoneSprcs
    @GetMapping("/findPhoneId")
    public PhoneInfo findPhoneId(Integer phoneId){
        return phoneService.findPhoneById(phoneId);
    }

    //通过specsId和购买数量，获取库存，并判断，是否够，不够抛异常，够修改库存
    @GetMapping("/findSpecsIdAnd")
    public void subStock(Integer specsId,Integer specsStock){
        phoneService.subStock(specsId,specsStock);
    }


}
